package com.zuxia.controller;

import com.zuxia.service.FileService;
import com.zuxia.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/file")
@Api("图片")
public class FileController {
    @Resource
    private FileService fileService;

    @ApiOperation("文件上传接口")
    @PostMapping("/upload/{type}")
    public R fileUpload(@PathVariable("type") String type, @RequestParam("file") MultipartFile file) throws Exception {
        System.out.println(type);
        System.out.println(file.getOriginalFilename());
        return R.ok().message("上传成功").data("teacher",fileService.upload(file.getInputStream(),type,file.getOriginalFilename()));
    }

    @ApiOperation(value = "文件删除")
    @DeleteMapping("/remove")
    public R removeFile(
            @ApiParam(value = "要删除的文件url路径", required = true)
            @RequestBody String url) throws Exception{
        fileService.removeFile(url);
        return R.ok().message("文件删除成功");
    }
}
